#!coding=utf-8
#ques
from tastypie.resources import ModelResource
from tastypie import fields
from weixin.models import User
from shop.models import Order, Catagory, Goods, OrderItem, Brand, CustAdr,\
    CustAssess, Format, SaleInfo, CardInfo, CardItem, Deduction, DeductionItem,\
    UserFavorite, Catagory_T
from tastypie.authentication import Authentication
from tastypie.authorization import Authorization
from chat.models import Servicer, Roomsta


class SillyAuthentication(Authentication):
    def is_authenticated(self, request, **kwargs):
        if 'daniel' in request.user.username:
            return True
        return False

class UserResource(ModelResource):
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "telephone": ('exact', 'startswith',),
            "openid": ('exact', 'startswith',),
            "password": ('exact', 'startswith',),
        }
#         contains
#         excludes = ['password', 'telephone']
        allowed_methods = ['get','post','put']
        queryset=User.objects.all()
        resource_name = 'user'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT

class ServicerResource(ModelResource):
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "status": ('exact', 'startswith',),
            "password": ('exact', 'startswith',),
            "nickname": ('exact', 'startswith',),
        }
        allowed_methods = ['get','put']
        queryset=Servicer.objects.all()
        resource_name = 'servicer'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
        
class RoomstaResource(ModelResource):
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "roomname": ('exact', 'startswith',),
            "roomsts": ('exact', 'startswith',),
            "receiver": ('exact', 'startswith',),
        }
        allowed_methods = ['get','post','put']
        queryset=Roomsta.objects.all()
        resource_name = 'roomsta'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT

class CustAdrResource(ModelResource):
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "wx_id": ('exact', 'startswith',),
            "phoneno": ('exact', 'startswith',),
        }
        allowed_methods = ['get','post','put']
        queryset=CustAdr.objects.all()
        resource_name = 'custadr'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
        
class OrdersResource(ModelResource):
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "open_id": ('exact', 'startswith',),
            "status": ('exact', 'startswith',),
        }
        allowed_methods = ['get','put']
        queryset=Order.objects.all().order_by('-id')
        resource_name = 'order'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
class CatagoryTResource(ModelResource):
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "name": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=Catagory_T.objects.all().order_by('-postion')
        resource_name = 'catagory'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT        
class CatagoryResource(ModelResource):
    catagory_T=fields.ForeignKey(CatagoryTResource,'catagory_T',full=True,null=True)
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "name": ('exact', 'startswith',),
            "catagory_T": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=Catagory.objects.all().order_by('-postion')
        resource_name = 'catagory'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT

class BrandResource(ModelResource):
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "name": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=Brand.objects.all()
        resource_name = 'brand'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
                

class SaleInfoResource(ModelResource):
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "salename": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=SaleInfo.objects.all()
        resource_name = 'saleinfo'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
        
class GoodsResource(ModelResource):
    catagory = fields.ForeignKey(CatagoryResource,'catagory',full=True)
    brand = fields.ForeignKey(BrandResource,'brand',full=True)
    saleinfo=fields.ForeignKey(SaleInfoResource,'saleinfo',full=True,null=True)
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "name": ('exact', 'contains',),
            "brand": ('exact', 'contains',),
            "catagory": ('exact', 'contains',),
            "saleinfo": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=Goods.objects.all().order_by('-postion')
        resource_name = 'good'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT

class GoodsResourceprice1(ModelResource):
    catagory = fields.ForeignKey(CatagoryResource,'catagory',full=True)
    brand = fields.ForeignKey(BrandResource,'brand',full=True)
    saleinfo=fields.ForeignKey(SaleInfoResource,'saleinfo',full=True,null=True)
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "name": ('exact', 'contains',),
            "brand": ('exact', 'contains',),
            "catagory": ('exact', 'contains',),
            "saleinfo": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=Goods.objects.all().order_by('price')
        resource_name = 'good_price1'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
        
class GoodsResourceprice2(ModelResource):
    catagory = fields.ForeignKey(CatagoryResource,'catagory',full=True)
    brand = fields.ForeignKey(BrandResource,'brand',full=True)
    saleinfo=fields.ForeignKey(SaleInfoResource,'saleinfo',full=True,null=True)
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "name": ('exact', 'contains',),
            "brand": ('exact', 'contains',),
            "catagory": ('exact', 'contains',),
            "saleinfo": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=Goods.objects.all().order_by('-price')
        resource_name = 'good_price2'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT

class GoodsResourcesale1(ModelResource):
    catagory = fields.ForeignKey(CatagoryResource,'catagory',full=True)
    brand = fields.ForeignKey(BrandResource,'brand',full=True)
    saleinfo=fields.ForeignKey(SaleInfoResource,'saleinfo',full=True,null=True)
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "name": ('exact', 'contains',),
            "brand": ('exact', 'contains',),
            "catagory": ('exact', 'contains',),
            "saleinfo": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=Goods.objects.all().order_by('sales')
        resource_name = 'good_sale1'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
class GoodsResourcesale2(ModelResource):
    catagory = fields.ForeignKey(CatagoryResource,'catagory',full=True)
    brand = fields.ForeignKey(BrandResource,'brand',full=True)
    saleinfo=fields.ForeignKey(SaleInfoResource,'saleinfo',full=True,null=True)
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "name": ('exact', 'contains',),
            "brand": ('exact', 'contains',),
            "catagory": ('exact', 'contains',),
            "saleinfo": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=Goods.objects.all().order_by('-sales')
        resource_name = 'good_sale2'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
        
class OrderItemResource(ModelResource):
    order = fields.ForeignKey(OrdersResource,'order',full=True)
    goods = fields.ForeignKey(GoodsResource,'good',full=True)
    class Meta:
        filtering = {
            "id":('exact', 'startswith',),
            "order":('exact', 'startswith',),
            "goods": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=OrderItem.objects.all()
        resource_name = 'orderitem'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT    
        
class CustAssessResource(ModelResource):
    goods = fields.ForeignKey(GoodsResource,'good',full=True)
    order= fields.ForeignKey(OrdersResource,'order',full=True)
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "wx_id": ('exact', 'startswith',),
            "goods": ('exact', 'startswith',),
            "order":('exact', 'startswith',),
        }
        allowed_methods = ['get','post','put']
        queryset=CustAssess.objects.all()
        resource_name = 'custassess'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
        
class FormatResource(ModelResource):
    goods = fields.ForeignKey(GoodsResource,'good',full=True)
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "goods": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=Format.objects.all()
        resource_name = 'format'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT

class CardInfoResource(ModelResource):
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=CardInfo.objects.all()
        resource_name = 'cardinfo'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
        
class CardItemResource(ModelResource):
    cardinfo = fields.ForeignKey(CardInfoResource,'cardinfo',full=True)
#     user = fields.ForeignKey(UserResource,'user',full=True)
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "cardinfo": ('exact', 'startswith',),
            "openid": ('exact', 'startswith',),
        }
        allowed_methods = ['get','post']
        queryset=CardItem.objects.all()
        resource_name = 'carditem'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
        
class DeductionResource(ModelResource):
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
        }
        allowed_methods = ['get']
        queryset=Deduction.objects.all()
        resource_name = 'deduction'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
        
class DeductionItemResource(ModelResource):
    deduction = fields.ForeignKey(DeductionResource,'deduction',full=True)
#     user = fields.ForeignKey(UserResource,'user',full=True)
    class Meta:
        filtering = {
            "id": ('exact', 'startswith',),
            "deduction": ('exact', 'startswith',),
            "openid": ('exact', 'startswith',),
            "status": ('exact', 'startswith',),
        }
        allowed_methods = ['get','post']
        queryset=DeductionItem.objects.all()
        resource_name = 'deductionitem'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
        
class UserFavoriteResource(ModelResource):
    goods = fields.ForeignKey(GoodsResource,'good',full=True)
    class Meta:
        filtering = {
            "openid": ('exact', 'startswith',),
            "goods": ('exact', 'startswith',),
        }
        allowed_methods = ['get','post']
        queryset=UserFavorite.objects.all().order_by('-modified')
        resource_name = 'userfavorite'
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization() # THIS IS IMPORTANT
